package com.lianxi.rabbitmq.exanges;


import com.lianxi.rabbitmq.util.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MyExangesTopics {

    public static void main(String[] args) {

    }
}


class ReceiveTopicLogs01 {
    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] argv) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        /**
         * 生成一个临时的队列 队列的名称是随机的
         * 当消费者断开和该队列的连接时 队列自动删除
         */
        String queueName = channel.queueDeclare().getQueue();
        //把该临时队列绑定我们的 exchange 其中 routingkey(也称之为 binding key)为空字符串
        channel.queueBind(queueName, EXCHANGE_NAME, "pingguo.*.*");
        System.out.println("pingguo.*.*等待接收消息,把接收到的消息打印在屏幕.....");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("控制台打印接收到的消息" + message);
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}

class ReceiveTopicLogs02 {
    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] argv) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        /**
         * 生成一个临时的队列 队列的名称是随机的
         * 当消费者断开和该队列的连接时 队列自动删除
         */
        String queueName = channel.queueDeclare().getQueue();
        //把该临时队列绑定我们的 exchange 其中 routingkey(也称之为 binding key)为空字符串
        channel.queueBind(queueName, EXCHANGE_NAME, "*.pingguo.*");
        System.out.println("*.pingguo.*等待接收消息,把接收到的消息写到文件.....");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
//            File file = new File("e:\\desktop\\rabbitmq_info.txt");
//            FileUtils.writeStringToFile(file, message, "UTF-8");
//            System.out.println("数据写入文件成功");
            System.out.println("控制台打印接收到的消息" + message);

        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}


class ReceiveTopicLogs03 {
    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] argv) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        /**
         * 生成一个临时的队列 队列的名称是随机的
         * 当消费者断开和该队列的连接时 队列自动删除
         */
        String queueName = channel.queueDeclare().getQueue();
        //把该临时队列绑定我们的 exchange 其中 routingkey(也称之为 binding key)为空字符串
        channel.queueBind(queueName, EXCHANGE_NAME, "xiangjiao.#");
        System.out.println("xiangjiao.#\"等待接收消息,把接收到的消息打印在屏幕.....");
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println("控制台打印接收到的消息" + message);
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}

class EmitTopicLog {
    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] argv) throws Exception {
        try (Channel channel = RabbitMqUtils.getChannel()) {
            /**
             * 声明一个 exchange
             * 1.exchange 的名称
             * 2.exchange 的类型
             */
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
            HashMap<String, String> bindingKeyMap = new HashMap<>();
            bindingKeyMap.put("pingguo.1.2", "pingguo.1.2信息");
            bindingKeyMap.put("1.pingguo.2", "1.pingguo.2信息");
            bindingKeyMap.put("xiangjiao.pingguo.3", "xiangjiao.pingguo.3信息");
            bindingKeyMap.put("xiangjiao.pingguo.3.pingguo", "xiangjiao.pingguo.3信息");
            bindingKeyMap.put("test","test消息");
            for (Map.Entry<String, String> stringStringEntry : bindingKeyMap.entrySet()) {
                channel.basicPublish(EXCHANGE_NAME, stringStringEntry.getKey(), null, stringStringEntry.getValue().getBytes("UTF-8"));
                System.out.println("生产者发出消息:" + stringStringEntry.getValue());
            }

        }
    }
}